﻿/* Copyright (C) 2008 - 2011 by John Cronin
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace tysos
{
    class CastClass
    {
        /* Black magic
         * 
         * Basically it relies on a fixed implementation of TysosType (you cannot create pointers to reference types
         * even in an usafe context so we cannot use the type directly).
         * 
         * Dereference the object to get the vtable
         * Dereference the vtable to get the TysosType instance
         * 
         * Compare the current TysosType and all base classes with dest_type_info
         * Then go back to the original TysosType structure and compare all its interfaces
         * 
         * Return obj if success, 0 otherwise
         */

        [ABI.MethodAlias("castclassex")]
        [ABI.AlwaysCompile()]
        public static ulong CastClassEx(ulong obj, ulong dest_type_info)
        {
            unsafe
            {
                if (obj == 0)
                    return 0;

                ulong* obj_pointer = (ulong *)obj;
                ulong* vt_pointer = (ulong *)*obj_pointer;

                ulong* cur_ti_pointer = (ulong *)*vt_pointer;
                ulong* orig_ti_pointer = cur_ti_pointer;

                while (cur_ti_pointer != null)
                {
                    if ((ulong)cur_ti_pointer == dest_type_info)
                        return obj;

                    cur_ti_pointer = (ulong*)*(ulong *)((byte *)cur_ti_pointer + 24);       // Extends field is at offset 24
                }

                ulong* iface_pointer = (ulong*)*(ulong*)((byte*)orig_ti_pointer + 48);      // Interfaces field is at offset 48

                if (iface_pointer == null)
                    return 0;

                while (*iface_pointer != 0)
                {
                    if (*iface_pointer == dest_type_info)
                        return obj;
                    iface_pointer++;
                }
            }

            return 0;
        }
    }
}
